package DataModel
{
	import WebServices.WebServiceFacade;
	
	import flash.errors.IllegalOperationError;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.DateFormatter;
	
	import tristate.TsState;

	/////////////////////////////////////////////////////////////////////////////////
	// Singleton class representing the one and only Document for this application
	/////////////////////////////////////////////////////////////////////////////////
	public class SkillsAuditDocument
	{
		//
		// Property allowing access to the singleton instance
		//
		static public function get instance():SkillsAuditDocument
		{
			return theDocument;						
		}
		
		static private var theDocument:SkillsAuditDocument = new SkillsAuditDocument(null);
		
		//
		// Constructor enforces singleton pattern
		//
		public function SkillsAuditDocument(doNotConstructThis:Object)
		{
			if ( theDocument != null )  
         	{  
  				throw new IllegalOperationError("Cannot instantiate new SkillsAuditDocument. Use SkillsAuditDocument.instance.");
			}
		}

		private var m_bDirtyForm:Boolean = false;
		
		[Bindable]
		public function get dirtyForm():Boolean
		{
			return m_bDirtyForm;
		}
		public function set dirtyForm(dirty:Boolean):void
		{
			m_bDirtyForm = dirty;
		}

		private var m_bInvalidForm:Boolean = false;
		
		[Bindable]
		public function get invalidForm():Boolean
		{
			return m_bInvalidForm;
		}
		public function set invalidForm(invalid:Boolean):void
		{
			m_bInvalidForm = invalid;
		}

		private var m_strAccessLevel:String;
		
		[Bindable]
		public function get accessLevel():String
		{
			return m_strAccessLevel;
		}
		public function set accessLevel(str:String):void
		{
			m_strAccessLevel = str;
		}

		private var m_bRejected:Boolean;
		
		[Bindable]
		public function get rejected():Boolean
		{
			return m_bRejected;
		}
		public function set rejected(boolReject:Boolean):void
		{
			m_bRejected = boolReject;
		}

		private var m_strVerifierComment:String;
		
		[Bindable]
		public function get verifierComment():String
		{
			return m_strVerifierComment;
		}
		public function set verifierComment(str:String):void
		{
			m_strVerifierComment = str;
		}

//
// Data members
//
		private var m_strDocumentNumber:String;
		private var m_intTransactionType:int;
		
		// Station id fields
		private var m_strMinistryId:String;
		private var m_strDepartmentId:String;
		private var m_strProvinceId:String;
		private var m_strDistrictId:String;
		private var m_strPayStationId:String;
		private var m_intAttendance:int;
		private var m_dateEnumeration:Date;
		private var m_strEnumeratorName:String;
		private var m_bEnumeratorSign:Boolean;
		// Personnel Id fields
		private var m_strTitle:String;
		private var m_strSurname:String;
		private var m_strFirstName:String;
		private var m_strOtherNames:String;
		private var m_strPreviousName:String;
		private var m_strNationalId:String;
		private var m_strNSSAId:String;
		private var m_strECNumber:String;
		private var m_strBirthEntryNumber:String;
		private var m_strMedicalAidID:String;
		private var m_strNameChangeAuth:String;
		// Personal Information fields
		private var m_bUseDateOfBirth:Boolean;
		private var m_dateDateOfBirth:Date;
		private var m_bUsePlaceOfBirth:Boolean;
		private var m_strPlaceOfBirth:String;
		private var m_bUseCity:Boolean;
		private var m_strCity:String;
		private var m_bUseDistrict:Boolean;
		private var m_strDistrict:String;
		private var m_bUseProvince:Boolean;
		private var m_strProvince:String;
		private var m_bUseCountry:Boolean;
		private var m_intCountry:int;
		private var m_bUseSex:Boolean;
		private var m_strSex:String;
		private var m_bUseMaritalStatus:Boolean;
		private var m_strMaritalStatus:String;
		private var m_bUseNationality:Boolean;
		private var m_intNationality:int;
		private var m_bNationalID:Boolean;
		private var m_bPassport:Boolean;
		private var m_bBirthCertificate:Boolean;
		private var m_intDocumentUsed:int;

		//ROE Fields
		private var m_bUseFirstAppointment:Boolean;
		private var m_dateFirstAppointment:Date;
		private var m_bUseAppointmentLetter:Boolean;
		private var m_dateAppointmentLetter:Date;
		private var m_bUseReappointment:Boolean;
		private var m_dateReappointment:Date;
		private var m_bUseCurrentGradeDate:Boolean;
		private var m_dateCurrentGrade:Date;
		private var m_bUseDesignation:Boolean;
		private var m_strDesignation:String
		private var m_bUseCurrentGrade:Boolean;
		private var m_strCurrentGrade:String
		private var m_bUseSalaryCode:Boolean;
		private var m_strSalaryCode:String
		private var m_bUseGrossSalary:Boolean;
		private var m_strGrossSalary:String;
		private var m_bUseMedicalClearance:Boolean;
		private var m_dateMedicalClearance:Date;
		private var m_bUsePoliceClearance:Boolean;
		private var m_datePoliceClearance:Date;
		private var m_bUseAppointmentForm:Boolean;
		private var m_dateAppointmentForm:Date;
		private var m_tsstatePassportVerified:TsState;
		private var m_tsstateBirthCertificateVerified:TsState;
		private var m_tsstateAppointmentLetterVerified:TsState;
		private var m_tsstateAppointmentFormVerified:TsState;
		private var m_tsstateMedicalClearanceVerified:TsState;
		private var m_tsstatePoliceClearanceVerified:TsState;

		// Contact Information fields
		private var m_strAddress:String;
		private var m_strContactDistrict:String;
		private var m_strContactProvince:String;
		private var m_strContactCity:String;
		private var m_strHomePhone:String;
		private var m_strWorkPhone:String;
		private var m_strMobilePhone:String;
		// Observations & Opinions fields
		private var m_strObserverName:String;
		private var m_dateObserverDate:Date;
		private var m_bObserverSign:Boolean;
		private var m_strOpinionGiverName:String;
		private var m_dateOpinionGiverDate:Date;
		private var m_bOpinionGiverSign:Boolean;
		//General Form fields
		private var m_bRespondentSign:Boolean;
		private var m_strAuditorName:String;
		private var m_bAuditorSign:Boolean;
		private var m_strDataEntryUserName:String;
		private var m_bDataEntryUserSign:Boolean;
		private var m_dateDataEntryDate:Date;
		private var m_strDataVerificationUserName:String;
		private var m_bDataVerificationUserSign:Boolean = false;
		private var m_dateDataVerificationDate:Date;
		private var m_intStatusCode:int;
		
		//Qualifications
		private var m_aAcademicQualifications:Array = [];
		private var m_aUniversityQualifications:Array = [];
		private var m_aProfessionalQualifications:Array = [];
		private var m_aInServiceTraining:Array = [];
		
		//Observations & Opinions
		private var m_aObservationsOpinions:Array = [];
		
		//Case History
		
		private var m_arrCaseHistory:Array = [];
		public var m_arrCaseActions:Array = [];
		
		private var m_CaseID:int;
		private var m_CaseType:String;
		private var m_hasChanged:Boolean;
		private var m_hasChangedAction:Boolean;
		private var m_CaseDate:Date;
		private var m_AssignedTo:String;
		private var m_CaseTypeID:int;
		private var m_ECNo:String;
		private var m_ModifiedBy:String;
		private var m_CreatedDate:Date;
		private var m_TotalCases:int;
		private var m_ActionDate:Date;
		private var m_ActionTaken:String;
		private var m_OfficialResponsible:String;
		private var m_Outcome:String;
		private var m_ID:int;
		private var m_CaseNumber:int;
		private var caseID:String;
//
// Properties and accessor functions
//
		[Bindable]
		public function get documentNumber():String
		{
			return m_strDocumentNumber;
		}
		private function set documentNumber(strDocNo:String):void
		{
			m_strDocumentNumber = strDocNo;
			if(strDocNo == "") // In this case we are clearing the document and it should not 
							   // be considered dirty.
			{
				dirtyForm = false;
			}
			else
			{
				dirtyForm = true;
			}
		}

		[Bindable]
		public function get transactionType():int
		{
			return m_intTransactionType;
		}
		private function set transactionType(strTransactionType:int):void
		{
			m_intTransactionType = strTransactionType;
			dirtyForm = true;
		}		

		[Bindable]
		public function get ministryId():String
		{
			return m_strMinistryId;
		}

		public function set ministryId(strId:String):void
		{
			m_strMinistryId = strId;
			dirtyForm = true;
		}
	
		// this uses strId instead of m_strMinistryId so that the bindable
		// ministryId property will appear in the mxml data binding expression,
		// allowing this to be updated appropriately.
		public function ministryName(strId:String):String
		{
			var strMinName:String = ReferenceData.ministries[strId.toUpperCase()] as String;
			if(Boolean(strMinName))
			{
				return strMinName;
			}
			else 
			{
				return "";
			}				
		}
	
		[Bindable]
		public function get departmentId():String
		{
			return m_strDepartmentId;
		}

		public function set departmentId(strId:String):void
		{
			m_strDepartmentId = strId;
			dirtyForm = true;
		}
	
		public function departmentName(strId:String):String
		{
			var strDeptName:String = ReferenceData.departments[strId.toUpperCase()] as String;
			if(Boolean(strDeptName))
			{
				return strDeptName;
			}
			else 
			{
				return "";
			}				
		}
		
		[Bindable]
		public function get provinceId():String
		{
			return m_strProvinceId;
		}

		public function set provinceId(strId:String):void
		{
			m_strProvinceId = strId;
			dirtyForm = true;
		}
	
		public function provinceName(strId:String):String
		{
			var strProvinceName:String = ReferenceData.provinces[strId.toUpperCase()] as String;
			if(Boolean(strProvinceName))
			{
				return strProvinceName;
			}
			else 
			{
				return "";
			}				
		}
		
		[Bindable]
		public function get districtId():String
		{
			return m_strDistrictId;
		}

		public function set districtId(strId:String):void
		{
			m_strDistrictId = strId;
			dirtyForm = true;
		}
	
		public function districtName(strId:String):String
		{
			var strDistrictName:String = ReferenceData.districts[strId.toUpperCase()] as String;
			if(Boolean(strDistrictName))
			{
				return strDistrictName;
			}
			else 
			{
				return "";
			}				
		}
		
		[Bindable]
		public function get payStationId():String
		{
			return m_strPayStationId;
		}

		public function set payStationId(strId:String):void
		{
			m_strPayStationId = strId;
			dirtyForm = true;
		}
	
		public function payStationName(strMinistryId:String, strDeptId:String, strPSId:String):String
		{
			var strKey:String = PayStation.makeKey(strMinistryId, strDeptId, strPSId);
			var payStation:PayStation = ReferenceData.payStations[strKey] as PayStation;
			if(Boolean(payStation))
			{
				return payStation.payStationDesc;
			}
			else 
			{
				return "";
			}				
		}

		[Bindable]
		public function get attendance():int
		{
			return m_intAttendance;
		}

		public function set attendance(intAtt:int):void
		{
			m_intAttendance = intAtt;
			dirtyForm = true;
		}

		[Bindable]
		public function get enumerationDate():Date
		{
			return m_dateEnumeration;
		}

		public function set enumerationDate(date:Date):void
		{
			m_dateEnumeration = date;
			dirtyForm = true;
		}

		[Bindable]
		public function get enumeratorName():String
		{
			return m_strEnumeratorName;
		}

		public function set enumeratorName(strName:String):void
		{
			m_strEnumeratorName = strName;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get enumeratorSign():Boolean
		{
			return m_bEnumeratorSign;
		}
		public function set enumeratorSign(bEnumeratorSign:Boolean):void
		{
			m_bEnumeratorSign = bEnumeratorSign;
			dirtyForm = true;
		}

		[Bindable]
		public function get title():String
		{
			return m_strTitle;
		}

		public function set title(strTitle:String):void
		{
			m_strTitle = strTitle;
			dirtyForm = true;
		}

		[Bindable]
		public function get surname():String
		{
			return m_strSurname;
		}

		public function set surname(strSurname:String):void
		{
			m_strSurname = strSurname;
			dirtyForm = true;
		}

		[Bindable]
		public function get firstName():String
		{
			return m_strFirstName;
		}

		public function set firstName(strFirstName:String):void
		{
			m_strFirstName = strFirstName;
			dirtyForm = true;
		}

		[Bindable]
		public function get otherNames():String
		{
			return m_strOtherNames;
		}

		public function set otherNames(strOtherNames:String):void
		{
			m_strOtherNames = strOtherNames;
			dirtyForm = true;
		}

		[Bindable]
		public function get previousName():String
		{
			return m_strPreviousName;
		}

		public function set previousName(strPreviousName:String):void
		{
			m_strPreviousName = strPreviousName;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get nationalId():String
		{
			return m_strNationalId;
		}

		public function set nationalId(strNationalId:String):void
		{
			m_strNationalId = strNationalId;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get nssaId():String
		{
			return m_strNSSAId;
		}

		public function set nssaId(strNSSAId:String):void
		{
			m_strNSSAId = strNSSAId;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get ecNumber():String
		{
			return m_strECNumber;
		}

		public function set ecNumber(strECNumber:String):void
		{
			m_strECNumber = strECNumber;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get birthEntryNumber():String
		{
			return m_strBirthEntryNumber;
		}

		public function set birthEntryNumber(strBirthEntryNumber:String):void
		{
			m_strBirthEntryNumber = strBirthEntryNumber;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get medicalAidID():String
		{
			return m_strMedicalAidID;
		}

		public function set medicalAidID(strMedicalAidID:String):void
		{
			m_strMedicalAidID = strMedicalAidID;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get nameChangeAuth():String
		{
			return m_strNameChangeAuth;
		}

		public function set nameChangeAuth(strNameChangeAuth:String):void
		{
			m_strNameChangeAuth = strNameChangeAuth;
			dirtyForm = true;
		}

		[Bindable]
		public function get useDateOfBirth():Boolean
		{
			return m_bUseDateOfBirth;
		}
		public function set useDateOfBirth(bUse:Boolean):void
		{
			m_bUseDateOfBirth = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get dateOfBirth():Date
		{
			return m_dateDateOfBirth;
		}

		public function set dateOfBirth(dateDateOfBirth:Date):void
		{
			m_dateDateOfBirth = dateDateOfBirth;
			dirtyForm = true;
		}

		[Bindable]
		public function get usePlaceOfBirth():Boolean
		{
			return m_bUsePlaceOfBirth;
		}
		public function set usePlaceOfBirth(bUse:Boolean):void
		{
			m_bUsePlaceOfBirth = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get placeOfBirth():String
		{
			return m_strPlaceOfBirth;
		}

		public function set placeOfBirth(strPlaceOfBirth:String):void
		{
			m_strPlaceOfBirth = strPlaceOfBirth;
			dirtyForm = true;
		}

		[Bindable]
		public function get useCity():Boolean
		{
			return m_bUseCity;
		}
		public function set useCity(bUse:Boolean):void
		{
			m_bUseCity = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get city():String
		{
			return m_strCity;
		}

		public function set city(strCity:String):void
		{
			m_strCity = strCity;
			dirtyForm = true;
		}

		[Bindable]
		public function get useDistrict():Boolean
		{
			return m_bUseDistrict;
		}
		public function set useDistrict(bUse:Boolean):void
		{
			m_bUseDistrict = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get district():String
		{
			return m_strDistrict;
		}

		public function set district(strDistrict:String):void
		{
			m_strDistrict = strDistrict;
			dirtyForm = true;
		}

		[Bindable]
		public function get useProvince():Boolean
		{
			return m_bUseProvince;
		}
		public function set useProvince(bUse:Boolean):void
		{
			m_bUseProvince = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get province():String
		{
			return m_strProvince;
		}

		public function set province(strProvince:String):void
		{
			m_strProvince = strProvince;
			dirtyForm = true;
		}		

		[Bindable]
		public function get useCountry():Boolean
		{
			return m_bUseCountry;
		}
		public function set useCountry(bUse:Boolean):void
		{
			m_bUseCountry = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get country():int
		{
			return m_intCountry;
		}

		public function set country(intCountry:int):void
		{
			m_intCountry = intCountry;
			dirtyForm = true;
		}		

		[Bindable]
		public function get useSex():Boolean
		{
			return m_bUseSex;
		}
		public function set useSex(bUse:Boolean):void
		{
			m_bUseSex = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get sex():String
		{
			switch(m_strSex)
			{
				case "1":
					return "M";
				case "2": 
					return "F";
				default:
					return " ";	
			}
		}

		public function set sex(strSex:String):void
		{
			if(strSex == "M" || strSex == "m" || strSex == "1") {
				m_strSex = "1";
			} else if (strSex == "F" || strSex == "f" || strSex == "2") {
				m_strSex = "2";
			} else { // 0 or other
				m_strSex = "0";
			}
			dirtyForm = true;
		}		

		[Bindable]
		public function get useMaritalStatus():Boolean
		{
			return m_bUseMaritalStatus;
		}
		public function set useMaritalStatus(bUse:Boolean):void
		{
			m_bUseMaritalStatus = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get maritalStatus():String
		{
			return m_strMaritalStatus;
		}

		public function set maritalStatus(strMaritalStatus:String):void
		{
			m_strMaritalStatus = strMaritalStatus;
			dirtyForm = true;
		}		

		[Bindable]
		public function get useNationality():Boolean
		{
			return m_bUseNationality;
		}
		public function set useNationality(bUse:Boolean):void
		{
			m_bUseNationality = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get nationality():int
		{
			return m_intNationality;
		}

		public function set nationality(intNationality:int):void
		{
			m_intNationality = intNationality;
			dirtyForm = true;
		}		

		[Bindable]
		public function get verifiedNationalID():Boolean
		{
			return m_bNationalID;
		}
		public function set verifiedNationalID(bUse:Boolean):void
		{
			m_bNationalID = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get verifiedPassport():Boolean
		{
			return m_bPassport;
		}
		public function set verifiedPassport(bUse:Boolean):void
		{
			m_bPassport = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get verifiedBirthCertificate():Boolean
		{
			return m_bBirthCertificate;
		}
		public function set verifiedBirthCertificate(bUse:Boolean):void
		{
			m_bBirthCertificate = bUse;
			dirtyForm = true;
		}

		//ROE Fields
		[Bindable]
		public function get useFirstAppointment():Boolean
		{
			return m_bUseFirstAppointment;
		}
		public function set useFirstAppointment(bUse:Boolean):void
		{
			m_bUseFirstAppointment = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get firstAppointment():Date
		{
			return m_dateFirstAppointment;
		}
		public function set firstAppointment(date:Date):void
		{
			m_dateFirstAppointment = date;
			dirtyForm = true;
		}

		[Bindable]
		public function get useAppointmentLetter():Boolean
		{
			return m_bUseAppointmentLetter;
		}
		public function set useAppointmentLetter(bUse:Boolean):void
		{
			m_bUseAppointmentLetter = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get appointmentLetterDate():Date
		{
			return m_dateAppointmentLetter;
		}
		public function set appointmentLetterDate(date:Date):void
		{
			m_dateAppointmentLetter = date;
			dirtyForm = true;
		}

		[Bindable]
		public function get useReappointment():Boolean
		{
			return m_bUseReappointment;
		}
		public function set useReappointment(bUse:Boolean):void
		{
			m_bUseReappointment = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get reappointmentDate():Date
		{
			return m_dateReappointment;
		}
		public function set reappointmentDate(date:Date):void
		{
			m_dateReappointment = date;
			dirtyForm = true;
		}

		[Bindable]
		public function get useCurrentGradeDate():Boolean
		{
			return m_bUseCurrentGradeDate;
		}
		public function set useCurrentGradeDate(bUse:Boolean):void
		{
			m_bUseCurrentGradeDate = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get currentGradeDate():Date
		{
			return m_dateCurrentGrade;
		}
		public function set currentGradeDate(date:Date):void
		{
			m_dateCurrentGrade = date;
			dirtyForm = true;
		}
	
		[Bindable]
		public function get useDesignation():Boolean
		{
			return m_bUseDesignation;
		}
		public function set useDesignation(bUse:Boolean):void
		{
			m_bUseDesignation = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get designation():String
		{
			return m_strDesignation;
		}

		public function set designation(strValue:String):void
		{
			m_strDesignation = strValue;
			dirtyForm = true;
		}		

		[Bindable]
		public function get useCurrentGrade():Boolean
		{
			return m_bUseCurrentGrade;
		}
		public function set useCurrentGrade(bUse:Boolean):void
		{
			m_bUseCurrentGrade = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get currentGrade():String
		{
			return m_strCurrentGrade;
		}

		public function set currentGrade(strValue:String):void
		{
			m_strCurrentGrade = strValue;
			dirtyForm = true;
		}		

		[Bindable]
		public function get useSalaryCode():Boolean
		{
			return m_bUseSalaryCode;
		}
		public function set useSalaryCode(bUse:Boolean):void
		{
			m_bUseSalaryCode = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get salaryCode():String
		{
			return m_strSalaryCode;
		}

		public function set salaryCode(strValue:String):void
		{
			m_strSalaryCode = strValue;
			dirtyForm = true;
		}		

		[Bindable]
		public function get useGrossSalary():Boolean
		{
			return m_bUseGrossSalary;
		}
		public function set useGrossSalary(bUse:Boolean):void
		{
			m_bUseGrossSalary = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get grossSalary():String
		{
			return m_strGrossSalary;
		}

		public function set grossSalary(strValue:String):void
		{
			m_strGrossSalary = strValue;
			dirtyForm = true;
		}		

		[Bindable]
		public function get useMedicalClearance():Boolean
		{
			return m_bUseMedicalClearance;
		}
		public function set useMedicalClearance(bUse:Boolean):void
		{
			m_bUseMedicalClearance = bUse;
			dirtyForm = true;
		}

		[Bindable]
		public function get medicalClearanceDate():Date
		{
			return m_dateMedicalClearance;
		}
		public function set medicalClearanceDate(date:Date):void
		{
			m_dateMedicalClearance = date;
			dirtyForm = true;
		}

		[Bindable]
		public function get usePoliceClearance():Boolean
		{
			return m_bUsePoliceClearance;
		}
		public function set usePoliceClearance(bUse:Boolean):void
		{
			m_bUsePoliceClearance = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get policeClearanceDate():Date
		{
			return m_datePoliceClearance;
		}
		public function set policeClearanceDate(date:Date):void
		{
			m_datePoliceClearance = date;
			dirtyForm = true;
		}

		[Bindable]
		public function get useAppointmentForm():Boolean
		{
			return m_bUseAppointmentForm;
		}
		public function set useAppointmentForm(bUse:Boolean):void
		{
			m_bUseAppointmentForm = bUse;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get appointmentFormDate():Date
		{
			return m_dateAppointmentForm;
		}
		public function set appointmentFormDate(date:Date):void
		{
			m_dateAppointmentForm = date;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get passportVerified():TsState
		{
			return m_tsstatePassportVerified;
		}
		public function set passportVerified(state:TsState):void
		{
			m_tsstatePassportVerified = state;
			dirtyForm = true;
		}

		[Bindable]
		public function get birthCertificateVerified():TsState
		{
			return m_tsstateBirthCertificateVerified;
		}
		public function set birthCertificateVerified(state:TsState):void
		{
			m_tsstateBirthCertificateVerified = state;
			dirtyForm = true;
		}

		[Bindable]
		public function get appointmentLetterVerified():TsState
		{
			return m_tsstateAppointmentLetterVerified;
		}
		public function set appointmentLetterVerified(state:TsState):void
		{
			m_tsstateAppointmentLetterVerified = state;
			dirtyForm = true;
		}

		[Bindable]
		public function get appointmentFormVerified():TsState
		{
			return m_tsstateAppointmentFormVerified;
		}
		public function set appointmentFormVerified(state:TsState):void
		{
			m_tsstateAppointmentFormVerified = state;
			dirtyForm = true;
		}

		[Bindable]
		public function get medicalClearanceVerified():TsState
		{
			return m_tsstateMedicalClearanceVerified;
		}
		public function set medicalClearanceVerified(state:TsState):void
		{
			m_tsstateMedicalClearanceVerified = state;
			dirtyForm = true;
		}

		[Bindable]
		public function get policeClearanceVerified():TsState
		{
			return m_tsstatePoliceClearanceVerified;
		}
		public function set policeClearanceVerified(state:TsState):void
		{
			m_tsstatePoliceClearanceVerified = state;
			dirtyForm = true;
		}

		[Bindable]
		public function get contactCity():String
		{
			return m_strContactCity;
		}

		public function set contactCity(strCCity:String):void
		{
			m_strContactCity = strCCity;
			dirtyForm = true;
		}

		[Bindable]
		public function get address():String
		{
			return m_strAddress;
		}

		public function set address(strAddress:String):void
		{
			m_strAddress = strAddress;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get contactDistrict():String
		{
			return m_strContactDistrict;
		}

		public function set contactDistrict(strCDistrict:String):void
		{
			m_strContactDistrict = strCDistrict;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get contactProvince():String
		{
			return m_strContactProvince;
		}

		public function set contactProvince(strCProvince:String):void
		{
			m_strContactProvince = strCProvince;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get homePhone():String
		{
			return m_strHomePhone;
		}
		public function set homePhone(strHomePhone:String):void
		{
			m_strHomePhone = strHomePhone;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get workPhone():String
		{
			return m_strWorkPhone;
		}
		public function set workPhone(strWorkPhone:String):void
		{
			m_strWorkPhone = strWorkPhone;
			dirtyForm = true;
		}

		[Bindable]
		public function get mobilePhone():String
		{
			return m_strMobilePhone;
		}
		public function set mobilePhone(strMobilePhone:String):void
		{
			m_strMobilePhone = strMobilePhone;
			dirtyForm = true;
		}

		[Bindable]
		public function get observerName():String
		{
			return m_strObserverName;
		}
		public function set observerName(strObserverName:String):void
		{
			m_strObserverName = strObserverName;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get observerDate():Date
		{
			return m_dateObserverDate;
		}
		public function set observerDate(dObserverDate:Date):void
		{
			m_dateObserverDate = dObserverDate;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get observerSign():Boolean
		{
			return m_bObserverSign;
		}
		public function set observerSign(bObserverSign:Boolean):void
		{
			m_bObserverSign = bObserverSign;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get opinionGiverName():String
		{
			return m_strOpinionGiverName;
		}
		public function set opinionGiverName(strOpinionGiverName:String):void
		{
			m_strOpinionGiverName = strOpinionGiverName;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get opinionGiverDate():Date
		{
			return m_dateOpinionGiverDate;
		}
		public function set opinionGiverDate(strOpinionGiverDate:Date):void
		{
			m_dateOpinionGiverDate = strOpinionGiverDate;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get opinionGiverSign():Boolean
		{
			return m_bOpinionGiverSign;
		}
		public function set opinionGiverSign(bOpinionGiverSign:Boolean):void
		{
			m_bOpinionGiverSign = bOpinionGiverSign;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get respondentSign():Boolean
		{
			return m_bRespondentSign;
		}
		public function set respondentSign(bRespondentSign:Boolean):void
		{
			m_bRespondentSign = bRespondentSign;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get auditorName():String
		{
			return m_strAuditorName;
		}
		public function set auditorName(strAuditorName:String):void
		{
			m_strAuditorName = strAuditorName;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get auditorSign():Boolean
		{
			return m_bAuditorSign;
		}
		public function set auditorSign(bAuditorSign:Boolean):void
		{
			m_bAuditorSign = bAuditorSign;
			dirtyForm = true;
		}

		[Bindable]
		public function get dataEntryUserName():String
		{
			return m_strDataEntryUserName;
		}
		public function set dataEntryUserName(strDataEntryUserName:String):void
		{
			m_strDataEntryUserName = strDataEntryUserName;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get dataEntryUserSign():Boolean
		{
			return m_bDataEntryUserSign;
		}
		public function set dataEntryUserSign(bDataEntryUserSign:Boolean):void
		{
			m_bDataEntryUserSign = bDataEntryUserSign;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get dataEntryDate():Date
		{
			return m_dateDataEntryDate;
		}

		public function set dataEntryDate(dateDataEntryDate:Date):void
		{
			m_dateDataEntryDate = dateDataEntryDate;
			dirtyForm = true;
		}

		[Bindable]
		public function get dataVerificationUserName():String
		{
			return m_strDataVerificationUserName;
		}
		public function set dataVerificationUserName(strDataVerificationUserName:String):void
		{
			m_strDataVerificationUserName = strDataVerificationUserName;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get dataVerificationUserSign():Boolean
		{
			return m_bDataVerificationUserSign;
		}
		public function set dataVerificationUserSign(bDataVerificationUserSign:Boolean):void
		{
			m_bDataVerificationUserSign = bDataVerificationUserSign;
			dirtyForm = true;
		}

		[Bindable]
		public function get dataVerificationDate():Date
		{
			return m_dateDataVerificationDate;
		}

		public function set dataVerificationDate(dateDataVerificationDate:Date):void
		{
			m_dateDataVerificationDate = dateDataVerificationDate;
			dirtyForm = true;
		}

		[Bindable]
		public function get statusCode():int
		{
			return m_intStatusCode;
		}

		public function set statusCode(intStatusCode:int):void
		{
			m_intStatusCode = intStatusCode;
			dirtyForm = true;
		}
		

		[Bindable]
		public function get academicQualifications():ArrayCollection
		{
			return new ArrayCollection( m_aAcademicQualifications );
		}
		
		private function set academicQualifications(val:ArrayCollection):void
		{
			if(	val.source != m_aAcademicQualifications)
			{
				m_aAcademicQualifications = val.source;
				dirtyForm = true;
			}
		}
		
		public function addAcademicQualification(qual:QualificationRecord):void
		{
			academicQualifications = new ArrayCollection( m_aAcademicQualifications.concat(qual));
		}
		
		public function removeAcademicQualification(qual:QualificationRecord):void
		{
			m_aAcademicQualifications.splice(m_aAcademicQualifications.indexOf(qual),1);
			academicQualifications = academicQualifications;
			dirtyForm = true;
		}

		[Bindable]
		public function get universityQualifications():ArrayCollection
		{
			return new ArrayCollection( m_aUniversityQualifications );
		}
		
		private function set universityQualifications(val:ArrayCollection):void
		{
			if(	val.source != m_aUniversityQualifications)
			{
				m_aUniversityQualifications = val.source;
				dirtyForm = true;
			}
		}
		
		public function addUniversityQualification(qual:QualificationRecord):void
		{
			universityQualifications = new ArrayCollection( m_aUniversityQualifications.concat(qual));
		}
		
		public function removeUniversityQualification(qual:QualificationRecord):void
		{
			m_aUniversityQualifications.splice(m_aUniversityQualifications.indexOf(qual),1);
			universityQualifications = universityQualifications;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get professionalQualifications():ArrayCollection
		{
			return new ArrayCollection( m_aProfessionalQualifications );
		}
		
		private function set professionalQualifications(val:ArrayCollection):void
		{
			if(	val.source != m_aProfessionalQualifications)
			{
				m_aProfessionalQualifications = val.source;
				dirtyForm = true;
			}
		}
		
		public function addProfessionalQualification(qual:QualificationRecord):void
		{
			professionalQualifications = new ArrayCollection(m_aProfessionalQualifications.concat(qual));
		}

		public function removeProfessionalQualification(qual:QualificationRecord):void
		{
			m_aProfessionalQualifications.splice(m_aProfessionalQualifications.indexOf(qual),1);
			professionalQualifications = professionalQualifications;
			dirtyForm = true;
		}

		[Bindable]
		public function get inServiceTraining():ArrayCollection
		{
			return new ArrayCollection( m_aInServiceTraining );
		}
		
		private function set inServiceTraining(val:ArrayCollection):void
		{
			if(	val.source != m_aInServiceTraining)
			{
				m_aInServiceTraining = val.source;
				dirtyForm = true;
			}
		}
		
		public function addInServiceQualification(qual:QualificationRecord):void
		{
			inServiceTraining = new ArrayCollection(m_aInServiceTraining.concat(qual));
		}

		public function removeInServiceQualification(qual:QualificationRecord):void
		{
			m_aInServiceTraining.splice(m_aInServiceTraining.indexOf(qual),1);
			inServiceTraining = inServiceTraining;
			dirtyForm = true;
		}
		
		[Bindable]
		public function get observationsOpinions():ArrayCollection
		{
			return new ArrayCollection( m_aObservationsOpinions );
		}

 		private function set observationsOpinions(val:ArrayCollection):void
		{
			if(m_aObservationsOpinions != val.source)
			{
				m_aObservationsOpinions = val.source;
				dirtyForm = true;
			}
		}
		
		public function addObservationsOpinions(obs:ObservationsOpinions):void
		{
			observationsOpinions = new ArrayCollection(m_aObservationsOpinions.concat(obs));
			dirtyForm = true;
		}

		public function removeObservationsOpinions(obs:ObservationsOpinions):void
		{
			m_aObservationsOpinions.splice(m_aObservationsOpinions.indexOf(obs),1);
			// self assignment to kick off data binding
			observationsOpinions = new ArrayCollection(m_aObservationsOpinions);
			dirtyForm = true;
		}
		
		//Case History
		
		[Bindable]
		public function get CaseHistoryOne():ArrayCollection
		{
			return new ArrayCollection( m_arrCaseHistory );
		}

 		private function set CaseHistoryOne(val:ArrayCollection):void
		{
			if(m_arrCaseHistory != val.source)
			{
				m_arrCaseHistory = val.source;
				dirtyForm = true;
			}
		}
		
		[Bindable]
		public function get CaseActionsOne():ArrayCollection
		{
			return new ArrayCollection( m_arrCaseActions );
		}

 		private function set CaseActionsOne(val:ArrayCollection):void
		{
			if(m_arrCaseActions != val.source)
			{
				m_arrCaseActions = val.source;
				dirtyForm = true;
			}
		}
		
		[Bindable]
		public function get CaseActionsOneFiltered():ArrayCollection
		{
			return new ArrayCollection();
		}

 		private function set CaseActionsOneFiltered(val:ArrayCollection):void
		{
			if(m_arrCaseActions != val.source)
			{
				m_arrCaseActions = val.source;
				dirtyForm = true;
			}
		}
		
		
		public function addCaseHistory(obs:CaseHistory):void
		{
			CaseHistoryOne = new ArrayCollection(m_arrCaseHistory.concat(obs));
			dirtyForm = true;
		}

		public function removeCaseHistory(obs:CaseHistory):void
		{
			m_arrCaseHistory.splice(m_arrCaseHistory.indexOf(obs),1);
			// self assignment to kick off data binding
			CaseHistoryOne = new ArrayCollection(m_arrCaseHistory);
			dirtyForm = true;
		}
		
		public function addCaseActions(obs:CaseActions):void
		{
			CaseActionsOne = new ArrayCollection(m_arrCaseActions.concat(obs));
			dirtyForm = true;
		}

		public function removeCaseActions(obs:CaseActions):void
		{
			m_arrCaseActions.splice(m_arrCaseActions.indexOf(obs),1);
			// self assignment to kick off data binding
			CaseActionsOne = new ArrayCollection(m_arrCaseActions);
			dirtyForm = true;
		}
		
		[Bindable]
		
		public function get arrCaseHistory():Array
		{
			return m_arrCaseHistory;
		}

		public function set arrCaseHistory(arrCaseHistory:Array):void
		{
			m_arrCaseHistory = arrCaseHistory;
			dirtyForm = true;
		}
		
		[Bindable]
		
		public function get arrCaseActions():Array
		{
			return m_arrCaseActions;
		}

		public function set arrCaseActions(arrCaseActions:Array):void
		{
			m_arrCaseActions = arrCaseActions;
			dirtyForm = true;
		}		
		
		//[Bindable]
		
		public function get CaseID():int
		{
			return m_CaseID;
		}

		public function set CaseID(CaseID:int):void
		{
			m_CaseID = CaseID;
			dirtyForm = true;
		}		
		
		[Bindable]
		public function get CaseType():String
		{
			return m_CaseType;
		}

		public function set CaseType(CaseType:String):void
		{
			m_CaseType = CaseType;
			dirtyForm = true;
		}		
		
		[Bindable]
		public function get hasChanged():Boolean
		{
			return m_hasChanged;
		}

		public function set hasChanged(hasChanged:Boolean):void
		{
			m_hasChanged = hasChanged;
			dirtyForm = true;
		}		
		
		[Bindable]
		public function get hasChangedAction():Boolean
		{
			return m_hasChangedAction;
		}

		public function set hasChangedAction(hasChangedAction:Boolean):void
		{
			m_hasChangedAction = hasChangedAction;
			dirtyForm = true;
		}	
		
		[Bindable]
		public function get CaseTypeID():int
		{
			return m_CaseTypeID;
		}

		public function set CaseTypeID(CaseTypeID:int):void
		{
			m_CaseTypeID = CaseTypeID;
			dirtyForm = true;
		}		
		
		[Bindable]
		public function get CreatedDate():Date
		{
			return m_CreatedDate;
		}

		public function set CreatedDate(CreatedDate:Date):void
		{
			m_CreatedDate = CreatedDate;
			dirtyForm = true;
		}		
		
		[Bindable]
		public function get ECNo():String
		{
			return m_ECNo;
		}

		public function set ECNo(ECNo:String):void
		{
			m_ECNo = ECNo;
			dirtyForm = true;
		}		
		
		
		
		[Bindable]
		public function get AssignedTo():String
		{
			return m_AssignedTo;
		}

		public function set AssignedTo(AssignedTo:String):void
		{
			m_AssignedTo = AssignedTo;
			dirtyForm = true;
		}		
		
		[Bindable]
		public function get ModifiedBy():String
		{
			return m_ModifiedBy;
		}

		public function set ModifiedBy(ModifiedBy:String):void
		{
			m_ModifiedBy = ModifiedBy;
			dirtyForm = true;
		}		
		
		[Bindable]
		public function get CaseDate():Date
		{
			return m_CaseDate;
		}

		public function set CaseDate(CaseDate:Date):void
		{
			m_CaseDate = CaseDate;
			dirtyForm = true;
		}		
		
		[Bindable]
		public function get ActionDate():Date
		{
			return m_ActionDate;
		}

		public function set ActionDate(ActionDate:Date):void
		{
			m_ActionDate = ActionDate;
			dirtyForm = true;
		}		

		[Bindable]
		public function get ActionTaken():String
		{
			return m_ActionTaken;
		}

		public function set ActionTaken(ActionTaken:String):void
		{
			m_ActionTaken = ActionTaken;
			dirtyForm = true;
		}	
		
		[Bindable]
		public function get OfficialResponsible():String
		{
			return m_OfficialResponsible;
		}

		public function set OfficialResponsible(OfficialResponsible:String):void
		{
			m_OfficialResponsible = OfficialResponsible;
			dirtyForm = true;
		}	
		
		[Bindable]
		public function get Outcome():String
		{
			return m_Outcome;
		}

		public function set Outcome(Outcome:String):void
		{
			m_Outcome = Outcome;
			dirtyForm = true;
		}	
		
		[Bindable]
		public function get ID():int
		{
			return m_ID;
		}

		public function set ID(ID:int):void
		{
			m_ID = ID;
			dirtyForm = true;
		}	
		
		//[Bindable]
		
		public function get CaseNumber():int
		{
			return m_CaseNumber;
		}

		public function set CaseNumber(CaseNumber:int):void
		{
			m_CaseNumber = CaseNumber;
			dirtyForm = true;
		}		
		
		public function getMaxObservationsOpinions(aObservationsOpinions:ArrayCollection):int
		{
			var maximum:int = 0;
			var obs:ObservationsOpinions = new ObservationsOpinions();
			for (var i:int=0; i<aObservationsOpinions.length; i++) {
            	obs = ObservationsOpinions(aObservationsOpinions.getItemAt(i));
            	if (obs.noteRef > maximum) {
            		maximum = obs.noteRef;   // new maximum
        		}
    		}
    		return maximum; 
		}
		

		public function stringToDateTime(strODBC:String):Date {
			if (strODBC != "") {
			    // convert various date parts into numbers
			    // ----------------------------------------------------------------
			    // get year and convert to a number
			    var strYear:String = strODBC.substr(0,4);
			    var numYear:Number = Number(strYear);
			    // get month and convert to a number
			    var strMonth:String = strODBC.substr(5,2);
			    var numMonth:Number = Number(strMonth);
			    // get day (i.e. date) and convert to a number
			    var strDate:String = strODBC.substr(8,2);
			    var numDate:Number = Number(strDate);
			    // get hour and convert to a number
			    var strHour:String = strODBC.substr(11,2);
			    var numHour:Number = Number(strHour);
			    // get minutes and convert to a number
			    var strMins:String = strODBC.substr(14,2);
			    var numMins:Number = Number(strMins);
			    // get seconds and convert to a number
			    var strSecs:String = strODBC.substr(17,2);
			    var numSecs:Number = Number(strSecs);
			    
			    // return a new date object with the proper date numbers
			    // ----------------------------------------------------------------
			    var thisDate:Date = new Date(numYear,(numMonth-1),numDate,numHour,numMins,numSecs);
			    return thisDate;
		 	} else
		 	{
		 		return null;
		 	}
		} // end string.stringToDate() method 

		public static function stringToBoolean($string:String):Boolean
		{
			return ($string.toLowerCase() == "true" || $string.toLowerCase() == "1");
		}

		public function get mode():ApplicationMode
		{
			if (accessLevel == "5")
			{
				return ApplicationMode.DataEntry; //Auditor can always give his opinion
			}
			else if (accessLevel == "4")
			{
				return ApplicationMode.ReadOnly; //Supervisor should only use rejected button and read
			}
			if (Boolean(dataVerificationUserName) && Boolean(dataEntryUserName) && !rejected)
			{
				return ApplicationMode.ReadOnly; //Doc is entered, verified and not rejected
			}
			else if (Boolean(dataEntryUserName) && accessLevel == "3"
				&& ((!rejected && !Boolean(dataVerificationUserName)) || rejected)
				&& dataEntryUserName.toUpperCase() ==  WebServiceFacade.instance.getCurrentUser().toUpperCase())
			{ //If Verifier entered the document as a data entry operator, read only
				return ApplicationMode.ReadOnly;
			}
			else if(Boolean(dataEntryUserName)
				&& dataEntryUserName.toUpperCase() !=  WebServiceFacade.instance.getCurrentUser().toUpperCase()
				&& ((!rejected && !Boolean(dataVerificationUserName)) || rejected)
				&& (accessLevel == "1" || accessLevel == "3"))
			{ //User is not the one who did the entry and is an admin or supervisor
				return ApplicationMode.Validation; // Doc is entered and [(Doc is rejected) or (not rejected & not verified)]
			}
			else
			{
				return ApplicationMode.DataEntry; // We let the user enter some data if he passes previous checks
			}
		}	

//
// XML conversion functions
//
		public function fromXML(xml:XML):void
		{
			// Delay setting the docNo till the end. The presence of a docNo is used to 
			// indicate that a valid document is present. Setting it before the other fields 
			// can cause problems with code fired by data bindings on the docNo.
			var docNo:String = xml.EnumData.DocumentNumber;
			transactionType = parseInt(xml.EnumData.TransactionType);

			ministryId = xml.EnumData.StationID.MinistryId;
			departmentId = xml.EnumData.StationID.DepartmentId;
			provinceId = xml.EnumData.StationID.ProvinceId;
			districtId = xml.EnumData.StationID.DistrictId;
			payStationId = xml.EnumData.StationID.PayStationId;
			attendance = xml.EnumData.StationID.Attendance;
			enumerationDate = stringToDateTime(xml.EnumData.StationID.EnumerationDate);
			enumeratorName = xml.EnumData.StationID.EnumerationName;
			enumeratorSign = stringToBoolean(xml.EnumData.StationID.EnumerationSign);

			title = xml.EnumData.PersonnelID.Title;
			surname = xml.EnumData.PersonnelID.Surname;
			firstName = xml.EnumData.PersonnelID.FirstName;
			otherNames = xml.EnumData.PersonnelID.OtherNames;
			previousName = xml.EnumData.PersonnelID.PreviousName;
			nationalId = xml.EnumData.PersonnelID.NationalId;
			nssaId = xml.EnumData.PersonnelID.NSSAId;
			ecNumber = xml.EnumData.PersonnelID.ECNumber;
			birthEntryNumber = xml.EnumData.PersonnelID.BirthEntryNo;
			medicalAidID = xml.EnumData.PersonnelID.MedicalAidId;
			nameChangeAuth = xml.EnumData.PersonnelID.Authorization;

			dateOfBirth = stringToDateTime(xml.EnumData.PersonalInfo.DateBirth);
		    useDateOfBirth = stringToBoolean(xml.EnumData.PersonalInfo.DateBirthCheckbox);
		    placeOfBirth = xml.EnumData.PersonalInfo.PlaceBirth;
		    usePlaceOfBirth = stringToBoolean(xml.EnumData.PersonalInfo.PlaceBirthCheckbox);
		    city = xml.EnumData.PersonalInfo.City;
		    useCity = stringToBoolean(xml.EnumData.PersonalInfo.CityCheckbox);
		    district = xml.EnumData.PersonalInfo.District;
		    useDistrict = stringToBoolean(xml.EnumData.PersonalInfo.DistrictCheckbox);
		    province = xml.EnumData.PersonalInfo.Province;
		    useProvince = stringToBoolean(xml.EnumData.PersonalInfo.ProvinceCheckbox);
		    country = xml.EnumData.PersonalInfo.Country;
		    useCountry = stringToBoolean(xml.EnumData.PersonalInfo.CountryCheckbox);
		    sex = xml.EnumData.PersonalInfo.Sex;
		    useSex = stringToBoolean(xml.EnumData.PersonalInfo.SexCheckbox);
		    maritalStatus = xml.EnumData.PersonalInfo.MaritalStatus;
		    useMaritalStatus = stringToBoolean(xml.EnumData.PersonalInfo.MaritalStatusCheckbox);
		    nationality = xml.EnumData.PersonalInfo.Nationality;
		    useNationality = stringToBoolean(xml.EnumData.PersonalInfo.NationalityCheckbox);
		    

		    
		    if(xml.EnumData.PersonalInfo.DocumentUsed.toString() != "")
		    {
		    	var documentUsed:int = parseInt(xml.EnumData.PersonalInfo.DocumentUsed);
		    	if (documentUsed == 1) {
		    		verifiedNationalID = true;
		    		verifiedPassport = false;
		    		verifiedBirthCertificate = false;
		    	} else if (documentUsed == 10) {
		    		verifiedNationalID = false;
		    		verifiedPassport = true;
		    		verifiedBirthCertificate = false;
		    	} else if (documentUsed == 11) {
		    		verifiedNationalID = true;
		    		verifiedPassport = true;
		    		verifiedBirthCertificate = false;
		    	} else if (documentUsed == 30) {
		    		verifiedNationalID = false;
		    		verifiedPassport = false;
		    		verifiedBirthCertificate = true;
		    	} else if (documentUsed == 31) {
		    		verifiedNationalID = true;
		    		verifiedPassport = false;
		    		verifiedBirthCertificate = true;
		    	} else if (documentUsed == 41) {
		    		verifiedNationalID = true;
		    		verifiedPassport = true;
		    		verifiedBirthCertificate = true;
		    	} else if (documentUsed == 40) {
		    		verifiedNationalID = false;
		    		verifiedPassport = true;
		    		verifiedBirthCertificate = true;
		    	} else {
		    		verifiedNationalID = false;
		    		verifiedPassport = false;
		    		verifiedBirthCertificate = false;
		    	}
		    }

		    address = xml.EnumData.ContactInfo.Address;
		    contactCity = xml.EnumData.ContactInfo.ContactCity;
		    contactDistrict = xml.EnumData.ContactInfo.ContactDistrict;
		    contactProvince = xml.EnumData.ContactInfo.ContactProvince;
		    homePhone = xml.EnumData.ContactInfo.HomePhone;
		    workPhone = xml.EnumData.ContactInfo.WorkPhone;
		    mobilePhone = xml.EnumData.ContactInfo.MobilePhone;

		    firstAppointment = stringToDateTime(xml.EnumData.RecordOfEmployment.DateFirstAppt);
		    useFirstAppointment = stringToBoolean(xml.EnumData.RecordOfEmployment.DateFirstApptCheckbox);
		    appointmentLetterDate = stringToDateTime(xml.EnumData.RecordOfEmployment.ApptLetterDate);
		    useAppointmentLetter = stringToBoolean(xml.EnumData.RecordOfEmployment.ApptLetterDateCheckbox);
		    reappointmentDate = stringToDateTime(xml.EnumData.RecordOfEmployment.DateReappt);
		    useReappointment = stringToBoolean(xml.EnumData.RecordOfEmployment.DateReapptCheckbox);
		    currentGradeDate = stringToDateTime(xml.EnumData.RecordOfEmployment.DateApptCurr);
		    useCurrentGradeDate = stringToBoolean(xml.EnumData.RecordOfEmployment.DateApptCurrCheckbox);
		    designation = xml.EnumData.RecordOfEmployment.Designation;
		    useDesignation = stringToBoolean(xml.EnumData.RecordOfEmployment.DesignationCheckbox);
		    currentGrade = xml.EnumData.RecordOfEmployment.CurrentGrade;
		    useCurrentGrade = stringToBoolean(xml.EnumData.RecordOfEmployment.CurrentGradeCheckbox);
		    salaryCode = xml.EnumData.RecordOfEmployment.SalaryCode;
		    useSalaryCode = stringToBoolean(xml.EnumData.RecordOfEmployment.SalaryCodeCheckbox);
		    grossSalary = xml.EnumData.RecordOfEmployment.GrossMonthSalary;
		    useGrossSalary = stringToBoolean(xml.EnumData.RecordOfEmployment.GrossMonthSalaryCheckbox);
		    medicalClearanceDate = stringToDateTime(xml.EnumData.RecordOfEmployment.MedicalClearFormDate);
		    useMedicalClearance = stringToBoolean(xml.EnumData.RecordOfEmployment.MedicalClearFormDateCheckbox);
		    policeClearanceDate = stringToDateTime(xml.EnumData.RecordOfEmployment.PoliceClearFormDate);
		    usePoliceClearance = stringToBoolean(xml.EnumData.RecordOfEmployment.PoliceClearFormDateCheckbox);
		    appointmentFormDate = stringToDateTime(xml.EnumData.RecordOfEmployment.AppointmentFormDate);
		    useAppointmentForm = stringToBoolean(xml.EnumData.RecordOfEmployment.ApptFormDateCheckbox);
		    passportVerified = TsState.fromXML(xml.EnumData.RecordOfEmployment.Passport);
		    appointmentLetterVerified = TsState.fromXML(xml.EnumData.RecordOfEmployment.AppointmentLetter);
		    medicalClearanceVerified = TsState.fromXML(xml.EnumData.RecordOfEmployment.MedicalClearForm);
		    birthCertificateVerified = TsState.fromXML(xml.EnumData.RecordOfEmployment.BirthCertificate);
		    appointmentFormVerified = TsState.fromXML(xml.EnumData.RecordOfEmployment.AppointmentForm);
		    policeClearanceVerified = TsState.fromXML(xml.EnumData.RecordOfEmployment.PoliceClearForm);
		    
		    m_aAcademicQualifications = [];
		    var lst:XMLList = xml.EnumData.AcademicQual;
			for each (var academicQual:XML in lst)
			{
				addAcademicQualification(new QualificationRecord(academicQual.InstitutionTypeId,
					academicQual.InstitutionName, academicQual.FieldId, academicQual.QualificationId,
					academicQual.YearCompleted, academicQual.DocumentName,
		    		TsState.fromXML(academicQual.DocumentState), parseInt(academicQual.id)));
		    }
		    m_aAcademicQualifications.sort(QualificationRecord.compare);
		    // Self assignment to make data binding happen
		    academicQualifications = academicQualifications;

			m_aUniversityQualifications = [];
		    lst = xml.EnumData.UniversityQual;
			for each (var univQual:XML in lst)
			{
				addUniversityQualification(new QualificationRecord("university",
					univQual.InstitutionName, univQual.Field, univQual.Qualification,
					univQual.YearCompleted, univQual.DocumentName,
		    		TsState.fromXML(univQual.DocumentState), parseInt(univQual.id)));
		    }
		    m_aUniversityQualifications.sort(QualificationRecord.compare);
		    universityQualifications = universityQualifications;

		    m_aProfessionalQualifications = [];
		    lst = xml.EnumData.ProfQual;
			for each (var profQual:XML in lst)
			{
				addProfessionalQualification(new QualificationRecord("professional",
					profQual.InstitutionName, profQual.Field, profQual.Qualification,
					profQual.YearCompleted, profQual.DocumentName,
		    		TsState.fromXML(profQual.DocumentState), parseInt(profQual.id)));
		    }
		    m_aProfessionalQualifications.sort(QualificationRecord.compare);
		    professionalQualifications = professionalQualifications;
		    
		    m_aInServiceTraining = []; 
		    lst = xml.EnumData.InServiceTraining;
			for each (var training:XML in lst)
			{
				addInServiceQualification(new QualificationRecord("training",
				    training.Institution, training.TrainingCourse, "training",
					training.YearCompleted, training.DocumentName,
		    		TsState.fromXML(training.DocumentState), parseInt(training.id)));
		    }
		    m_aInServiceTraining.sort(QualificationRecord.compare);
		    inServiceTraining = inServiceTraining;

			m_aObservationsOpinions = [];
		    lst = xml.EnumData.Notes.Note;
			for each (var note:XML in lst)
			{
				addObservationsOpinions(new ObservationsOpinions(note.NoteRef,
				note.Observation, note.Opinion, note.RelatedSection));
		    }
			observationsOpinions = observationsOpinions;
			
			//History
			m_arrCaseHistory = [];
		    lst = xml.EnumData.CaseHistory.Case;
			for each (var acase:XML in lst)
			{
				addCaseHistory(new CaseHistory(stringToDateTime(acase.Date), acase.CaseID, acase.AssignedTo,
				acase.CaseTypeID, stringToDateTime(acase.CreatedDate), acase.ECNo, acase.ModifiedBy, false));
		    }
		 	
		 	
			CaseHistoryOne = CaseHistoryOne;

			//History Action
			
			m_arrCaseActions = [];
		    lst = xml.EnumData.CaseActionData.ActionData;
			for each (var acaseAction:XML in lst)
			{
				addCaseActions(new CaseActions(acaseAction.OfficialResponsible, acaseAction.ID,
				acaseAction.Outcome, acaseAction.ActionTaken, stringToDateTime(acaseAction.Date), acaseAction.CaseNumber, acaseAction.CreatedBy, stringToDateTime(acaseAction.CreatedDate), acaseAction.ModifiedBy, false));
		    }
		 	
		 	
			CaseActionsOne = CaseActionsOne;

		    observerDate = stringToDateTime(xml.EnumData.Notes.DateObservations);
		    observerName = xml.EnumData.Notes.ObserverName;
		    observerSign = stringToBoolean(xml.EnumData.Notes.ObserverSign);
		    opinionGiverDate = stringToDateTime(xml.EnumData.Notes.DateOpinionGiven);
		    opinionGiverName = xml.EnumData.Notes.OpinionGiverName;
		    opinionGiverSign = stringToBoolean(xml.EnumData.Notes.OpinionGiverSign);



		    //History
		    
		    CaseDate = stringToDateTime(xml.EnumData.Case.Date);
		    AssignedTo = xml.EnumData.Case.AssignedTo;
		    CaseID = xml.EnumData.Case.CaseID;
		    CaseType = xml.EnumData.Case.CaseTypeID;
		    hasChanged = xml.EnumData.Case.hasChanged;
		    
		    //Actions
		    
		  	ActionDate = stringToDateTime(xml.EnumData.CaseAction.Date);
			ActionTaken = xml.EnumData.CaseAction.ActionTaken;
			OfficialResponsible = xml.EnumData.CaseAction.OfficialResponsible;
			Outcome = xml.EnumData.CaseAction.Outcome;
			CaseNumber = xml.EnumData.CaseAction.CaseNumber;
			hasChanged = xml.EnumData.CaseAction.hasChanged;

		    respondentSign = stringToBoolean(xml.EnumData.RespondentSign);
		    auditorName = xml.EnumData.AuditorName;
		    auditorSign = stringToBoolean(xml.EnumData.AuditorSign);
		    dataEntryUserName = xml.EnumData.DataEntryUserName;
		    dataEntryUserSign = stringToBoolean(xml.EnumData.DataEntryUserSign);
		    dataEntryDate = stringToDateTime(xml.EnumData.DataEntryDate);
		    dataVerificationUserName = xml.EnumData.DataVerificationUserName;
		    dataVerificationUserSign = stringToBoolean(xml.EnumData.DataVerificationUserSign);
		    dataVerificationDate = stringToDateTime(xml.EnumData.DataVerificationDate);
		    statusCode = parseInt(xml.EnumData.StatusCode);
		    rejected = stringToBoolean(xml.EnumData.Rejected);
		    verifierComment = xml.EnumData.VerifierComment;

		    ReferenceData.acadInstitutions = ReferenceData.loadEditableList(xml.AcadInstitutions.Name);
			ReferenceData.univInstitutions = ReferenceData.loadEditableList(xml.UnivInstituts.Name);
			ReferenceData.univFields = ReferenceData.loadEditableList(xml.UnivFields.Name);
			ReferenceData.univQuals = ReferenceData.loadEditableList(xml.UnivQuals.Name); 
			ReferenceData.profInstitutions = ReferenceData.loadEditableList(xml.ProfInstituts.Name);
			ReferenceData.profFields = ReferenceData.loadEditableList(xml.ProfFields.Name);
			ReferenceData.profQuals = ReferenceData.loadEditableList(xml.ProfQuals.Name); 
			ReferenceData.trainingInstitutions = ReferenceData.loadEditableList(xml.TrainingInstituts.Name);
			ReferenceData.trainingCourses = ReferenceData.loadEditableList(xml.TrainingCourses.Name);

		    documentNumber = docNo;

		    dirtyForm = false;
		}

		public function booleanToInt(boolToInt:Boolean):int
		{
    		if (boolToInt) {
        		return 1;
    		}
    		return 0;
		}
		
		public function toXML():XML
		{
			var df:DateFormatter = new DateFormatter();
			df.formatString = "YYYY-MM-DD";

			var dfTimestamp:DateFormatter = new DateFormatter();
			dfTimestamp.formatString = "YYYY-MM-DD HH:NN:SS";
			
			var xml:XML = 
			  <EnumData>
			    <DocumentNumber></DocumentNumber>
			    <TransactionType></TransactionType>
			    <StationID>
			      <MinistryId></MinistryId>
			      <MinistryName></MinistryName>
			      <DepartmentId></DepartmentId>
			      <DepartmentName></DepartmentName>
			      <ProvinceId></ProvinceId>
			      <ProvinceName></ProvinceName>
			      <DistrictId></DistrictId>
			      <District></District>
			      <PayStationId></PayStationId>
			      <PayStationName></PayStationName>
			      <Attendance></Attendance>
			      <EnumerationDate></EnumerationDate>
			      <EnumerationName></EnumerationName>
			      <EnumerationSign></EnumerationSign>
			    </StationID>
			    <PersonnelID>
			      <Title></Title>
			      <Surname></Surname>
			      <FirstName></FirstName>
			      <OtherNames></OtherNames>
			      <PreviousName></PreviousName>
			      <NationalId></NationalId>
			      <NSSAId></NSSAId>
			      <ECNumber></ECNumber>
			      <BirthEntryNo></BirthEntryNo>
			      <MedicalAidId></MedicalAidId>
			      <Authorization></Authorization>
			    </PersonnelID>
			    <PersonalInfo>
			      <DateBirth></DateBirth>
			      <DateBirthCheckbox></DateBirthCheckbox>
			      <PlaceBirth></PlaceBirth>
			      <PlaceBirthCheckbox></PlaceBirthCheckbox>
			      <City></City>
			      <CityCheckbox></CityCheckbox>
			      <District></District>
			      <DistrictCheckbox></DistrictCheckbox>
			      <Province></Province>
			      <ProvinceCheckbox></ProvinceCheckbox>
			      <Country></Country>
			      <CountryCheckbox></CountryCheckbox>
			      <Sex></Sex>
			      <SexCheckbox></SexCheckbox>
			      <MaritalStatus></MaritalStatus>
			      <MaritalStatusCheckbox></MaritalStatusCheckbox>
			      <Nationality></Nationality>
			      <NationalityCheckbox></NationalityCheckbox>
			      <DocumentUsed></DocumentUsed>
			    </PersonalInfo>
			    <ContactInfo>
			      <Address></Address>
			      <ContactCity></ContactCity>
			      <ContactDistrict></ContactDistrict>
			      <ContactProvince></ContactProvince>
			      <HomePhone></HomePhone>
			      <WorkPhone></WorkPhone>
			      <MobilePhone></MobilePhone>
			    </ContactInfo>
			    <RecordOfEmployment>
			      <DateFirstAppt></DateFirstAppt>
			      <DateFirstApptCheckbox></DateFirstApptCheckbox>
			      <ApptLetterDate></ApptLetterDate>
			      <ApptLetterDateCheckbox></ApptLetterDateCheckbox>
			      <DateReappt></DateReappt>
			      <DateReapptCheckbox></DateReapptCheckbox>
			      <DateApptCurr></DateApptCurr>
			      <DateApptCurrCheckbox></DateApptCurrCheckbox>
			      <Designation></Designation>
			      <DesignationCheckbox></DesignationCheckbox>
			      <CurrentGrade></CurrentGrade>
			      <CurrentGradeCheckbox></CurrentGradeCheckbox>
			      <SalaryCode></SalaryCode>
			      <SalaryCodeCheckbox></SalaryCodeCheckbox>
			      <GrossMonthSalary></GrossMonthSalary>
			      <GrossMonthSalaryCheckbox></GrossMonthSalaryCheckbox>
			      <MedicalClearFormDate></MedicalClearFormDate>
			      <MedicalClearFormDateCheckbox></MedicalClearFormDateCheckbox>
			      <PoliceClearFormDate></PoliceClearFormDate>
			      <PoliceClearFormDateCheckbox></PoliceClearFormDateCheckbox>
			      <AppointmentFormDate></AppointmentFormDate>
			      <ApptFormDateCheckbox></ApptFormDateCheckbox>
			      <Passport></Passport>
			      <AppointmentLetter></AppointmentLetter>
			      <MedicalClearForm></MedicalClearForm>
			      <BirthCertificate></BirthCertificate>
			      <AppointmentForm></AppointmentForm>
			      <PoliceClearForm></PoliceClearForm>
			    </RecordOfEmployment>
			    <Notes>
			      <DateObservations></DateObservations>
			      <ObserverName></ObserverName>
			      <ObserverSign></ObserverSign>
			      <DateOpinionGiven></DateOpinionGiven>
			      <OpinionGiverName></OpinionGiverName>
			      <OpinionGiverSign></OpinionGiverSign>
			    </Notes>
			    <RespondentSign></RespondentSign>
			    <AuditorName></AuditorName>
			    <AuditorSign></AuditorSign>
			    <DataEntryUserName></DataEntryUserName>
			    <DataEntryUserSign></DataEntryUserSign>
			    <DataEntryDate></DataEntryDate>
			    <DataVerificationUserName></DataVerificationUserName>
			    <DataVerificationUserSign></DataVerificationUserSign>
			    <DataVerificationDate></DataVerificationDate>
			    <StatusCode></StatusCode>
			  </EnumData>;

			  var xmlAcademicTemplate:XML =
			    <AcademicQual>
			      <id/>
			      <InstitutionTypeId></InstitutionTypeId>
			      <InstitutionName></InstitutionName>
			      <FieldId></FieldId>
			      <QualificationId></QualificationId>
			      <YearCompleted></YearCompleted>
			      <DocumentName></DocumentName>
			      <DocumentState></DocumentState>
			    </AcademicQual>;
			  
			  var xmlProfessionalTemplate:XML =
			    <ProfQual>
			      <id/>
			      <InstitutionName></InstitutionName>
			      <Field></Field>
			      <Qualification></Qualification>
			      <YearCompleted></YearCompleted>
			      <DocumentName></DocumentName>
			      <DocumentState></DocumentState>
			    </ProfQual>;
			  
			  var xmlUniversityTemplate:XML =
			    <UniversityQual>
			      <id/>
			      <InstitutionName></InstitutionName>
			      <Field></Field>
			      <Qualification></Qualification>
			      <YearCompleted></YearCompleted>
			      <DocumentName></DocumentName>
			      <DocumentState></DocumentState>
			    </UniversityQual>; 
			  
			  var xmlInServiceTemplate:XML =
			    <InServiceTraining>
			      <id/>
			      <Institution></Institution>
			      <TrainingCourse></TrainingCourse>
			      <YearCompleted></YearCompleted>
			      <DocumentName></DocumentName>
			      <DocumentState></DocumentState>
			    </InServiceTraining>;

			  var xmlNoteTemplate:XML =
			      <Note>
			        <RelatedSection></RelatedSection>
			        <NoteRef></NoteRef>
			        <Observation></Observation>
			        <Opinion></Opinion>
			      </Note>;
			      
			      //History
			      
			   var xmlCaseHistoryTemplate:XML = 
			    <Cases>
			      <Date></Date>
			      <AssignedTo></AssignedTo>
			      <CreatedDate></CreatedDate>
			      <ModifiedBy></ModifiedBy>
			      <CaseID></CaseID>
			      <ECNo></ECNo>
			      <CaseTypeID></CaseTypeID>
			      <hasChanged></hasChanged>
			    </Cases> ;
			    
			    var xmlCaseActionsTemplate:XML = 
			    <CaseActions>
			      <Date></Date>
			      <CreatedBy></CreatedBy>
			      <OfficialResponsible></OfficialResponsible>
			      <Outcome></Outcome>
			      <CreatedDate></CreatedDate>
			      <ActionTaken></ActionTaken>
			      <ModifiedBy></ModifiedBy>
			      <ID></ID>
			      <CaseID></CaseID>
			      <hasChanged></hasChanged>
			    </CaseActions>;
			      
			xml.DocumentNumber = documentNumber;
			xml.TransactionType = 0; //transactionType;
			xml.StationID.MinistryId = ministryId;
			xml.StationID.MinistryName = ministryName(ministryId);
			xml.StationID.DepartmentId = departmentId;
			xml.StationID.DepartmentName = departmentName(departmentId);
			xml.StationID.ProvinceId = provinceId;
			xml.StationID.ProvinceName = provinceName(provinceId);
			xml.StationID.DistrictId = districtId;
			xml.StationID.District = districtName(districtId.toUpperCase());
			xml.StationID.PayStationId = payStationId;
			xml.StationID.PayStationName = payStationName(ministryId, departmentId, payStationId);
			xml.StationID.Attendance = attendance;
			xml.StationID.EnumerationDate = df.format(enumerationDate);
			xml.StationID.EnumerationName = enumeratorName;
			xml.StationID.EnumerationSign = booleanToInt(enumeratorSign);

		    xml.PersonnelID.Title = title;
		    xml.PersonnelID.Surname = surname;
		    xml.PersonnelID.FirstName = firstName;
		    xml.PersonnelID.OtherNames = otherNames;
		    xml.PersonnelID.PreviousName = previousName;
		    if(nationalId == "")
		    {
		    	delete xml.PersonnelID.NationalId;
		    }
		    else
		    {
		    	xml.PersonnelID.NationalId = nationalId;
		    }
		    xml.PersonnelID.NSSAId = nssaId;
		    if(ecNumber == "")
		    {
		    	delete xml.PersonnelID.ECNumber;
		    }
		    else
		    {
			    xml.PersonnelID.ECNumber = ecNumber;
		    }
		    xml.PersonnelID.BirthEntryNo = birthEntryNumber;
		    if(medicalAidID == "")
		    {
		    	delete xml.PersonnelID.MedicalAidId;
		    }
		    else
		    {
			    xml.PersonnelID.MedicalAidId = medicalAidID;
		    }
		    xml.PersonnelID.Authorization = nameChangeAuth;
			
			if(dateOfBirth == null || isNaN(dateOfBirth.date))
		    {
		    	delete xml.PersonalInfo.DateBirth;
		    }
		    else
		    {
				xml.PersonalInfo.DateBirth = df.format(dateOfBirth);
		    }
			xml.PersonalInfo.DateBirthCheckbox = booleanToInt(useDateOfBirth);
			xml.PersonalInfo.PlaceBirth = placeOfBirth;
			xml.PersonalInfo.PlaceBirthCheckbox = booleanToInt(usePlaceOfBirth);
			xml.PersonalInfo.City = city;
	      	xml.PersonalInfo.CityCheckbox = booleanToInt(useCity);
	      	xml.PersonalInfo.District = district
	      	xml.PersonalInfo.DistrictCheckbox = booleanToInt(useDistrict);
	      	xml.PersonalInfo.Province = province;
	      	xml.PersonalInfo.ProvinceCheckbox = booleanToInt(useProvince);
	      	xml.PersonalInfo.Country = country;
	      	xml.PersonalInfo.CountryCheckbox = booleanToInt(useCountry);
		    if(m_strSex != "0" && m_strSex != "1" && m_strSex != "2")
		    {
		    	delete xml.PersonalInfo.Sex;
		    }
		    else
		    {
		      	xml.PersonalInfo.Sex = m_strSex; //don't use getter. variable is 0/1, getter is m/f
		    }
	      	xml.PersonalInfo.SexCheckbox = booleanToInt(useSex);
	      	xml.PersonalInfo.MaritalStatus = maritalStatus;	
	      	xml.PersonalInfo.MaritalStatusCheckbox = booleanToInt(useMaritalStatus);
	      	xml.PersonalInfo.Nationality = nationality;
	      	xml.PersonalInfo.NationalityCheckbox = booleanToInt(useNationality);

	      	var documentUsed:int;
	      	if (verifiedNationalID)
	      	{
	      		documentUsed = documentUsed + 1;
	      	}
	      	if (verifiedPassport)
	      	{
	      		documentUsed = documentUsed + 10;
	      	}
	      	if (verifiedBirthCertificate)
	      	{
	      		documentUsed = documentUsed + 30;
	      	}
	      	xml.PersonalInfo.DocumentUsed = documentUsed;

		    xml.ContactInfo.Address = address;
		    xml.ContactInfo.ContactCity = contactCity;
		    xml.ContactInfo.ContactDistrict = contactDistrict;
		    xml.ContactInfo.ContactProvince = contactProvince;
		    xml.ContactInfo.HomePhone = homePhone;
		    xml.ContactInfo.WorkPhone = workPhone;
		    xml.ContactInfo.MobilePhone = mobilePhone;
		    
			if(firstAppointment == null || isNaN(firstAppointment.date))
		    {
		    	delete xml.RecordOfEmployment.DateFirstAppt;
		    }
		    else
		    {
	    		xml.RecordOfEmployment.DateFirstAppt = df.format(firstAppointment);
	    	}
      		xml.RecordOfEmployment.DateFirstApptCheckbox = booleanToInt(useFirstAppointment);
			if(appointmentLetterDate == null || isNaN(appointmentLetterDate.date))
		    {
		    	delete xml.RecordOfEmployment.ApptLetterDate;
		    }
		    else
		    {
				xml.RecordOfEmployment.ApptLetterDate = df.format(appointmentLetterDate);
		    }
			xml.RecordOfEmployment.ApptLetterDateCheckbox = booleanToInt(useAppointmentLetter);
			if(reappointmentDate == null || isNaN(reappointmentDate.date))
		    {
		    	delete xml.RecordOfEmployment.DateReappt;
		    }
		    else
		    {
				xml.RecordOfEmployment.DateReappt = df.format(reappointmentDate);
		    }
			xml.RecordOfEmployment.DateReapptCheckbox = booleanToInt(useReappointment);
			if(currentGradeDate == null || isNaN(currentGradeDate.date))
		    {
		    	delete xml.RecordOfEmployment.DateApptCurr;
		    }
		    else
		    {
				xml.RecordOfEmployment.DateApptCurr = df.format(currentGradeDate);
		    }
			xml.RecordOfEmployment.DateApptCurrCheckbox = booleanToInt(useCurrentGradeDate);
			xml.RecordOfEmployment.Designation = designation;
			xml.RecordOfEmployment.DesignationCheckbox = booleanToInt(useDesignation);
			xml.RecordOfEmployment.CurrentGrade = currentGrade;
			xml.RecordOfEmployment.CurrentGradeCheckbox = booleanToInt(useCurrentGrade);
			if(salaryCode == "")
		    {
		    	delete xml.RecordOfEmployment.SalaryCode;
		    }
		    else
		    {
				xml.RecordOfEmployment.SalaryCode = salaryCode;
		    }
			xml.RecordOfEmployment.SalaryCodeCheckbox = booleanToInt(useSalaryCode);
			if(grossSalary == "")
		    {
		    	delete xml.RecordOfEmployment.GrossMonthSalary;
		    }
		    else
		    {
				xml.RecordOfEmployment.GrossMonthSalary = grossSalary;
		    }
			xml.RecordOfEmployment.GrossMonthSalaryCheckbox = booleanToInt(useGrossSalary);
			if(medicalClearanceDate == null || isNaN(medicalClearanceDate.date))
		    {
		    	delete xml.RecordOfEmployment.MedicalClearFormDate;
		    }
		    else
		    {
				xml.RecordOfEmployment.MedicalClearFormDate = df.format(medicalClearanceDate);
		    }
			xml.RecordOfEmployment.MedicalClearFormDateCheckbox = booleanToInt(useMedicalClearance);
			if(policeClearanceDate == null || isNaN(policeClearanceDate.date))
		    {
		    	delete xml.RecordOfEmployment.PoliceClearFormDate;
		    }
		    else
		    {
				xml.RecordOfEmployment.PoliceClearFormDate = df.format(policeClearanceDate);
		    }
			xml.RecordOfEmployment.PoliceClearFormDateCheckbox = booleanToInt(usePoliceClearance);
			if(appointmentFormDate == null || isNaN(appointmentFormDate.date))
		    {
		    	delete xml.RecordOfEmployment.AppointmentFormDate;
		    }
		    else
		    {
				xml.RecordOfEmployment.AppointmentFormDate = df.format(appointmentFormDate);
		    }
			xml.RecordOfEmployment.ApptFormDateCheckbox = booleanToInt(useAppointmentForm);
			xml.RecordOfEmployment.Passport = passportVerified.toInt();
			xml.RecordOfEmployment.AppointmentLetter = appointmentLetterVerified.toInt();
			xml.RecordOfEmployment.MedicalClearForm = medicalClearanceVerified.toInt();
			xml.RecordOfEmployment.BirthCertificate = birthCertificateVerified.toInt();
			xml.RecordOfEmployment.AppointmentForm = appointmentFormVerified.toInt();
			xml.RecordOfEmployment.PoliceClearForm = policeClearanceVerified.toInt();
		    
		    var idAc:int = 0;
		    for each (var academic:QualificationRecord in academicQualifications)
		    {
		    	var xmlAcademic:XML = xmlAcademicTemplate.copy();

				xmlAcademic.id = idAc++;
		    	xmlAcademic.InstitutionTypeId = academic.institutionType;
		    	xmlAcademic.InstitutionName = academic.institution;
		    	xmlAcademic.FieldId = academic.field;
		    	xmlAcademic.QualificationId = academic.qualification;
			    if(academic.yearCompleted == "")
			    {
			    	delete xmlAcademic.YearCompleted;
			    }
			    else
			    {
			    	xmlAcademic.YearCompleted = academic.yearCompleted;
			    }
		    	xmlAcademic.DocumentName = academic.documents;
		    	xmlAcademic.DocumentState = academic.documentState.toInt();
		    	
		    	xml.insertChildBefore(xml.Notes, xmlAcademic);
		    }

		    var idUniv:int = 0;
		    for each (var university:QualificationRecord in universityQualifications)
		    {
		    	var xmlUniversity:XML = xmlUniversityTemplate.copy();

		    	xmlUniversity.id = idUniv++;
		    	xmlUniversity.InstitutionName = university.institution;
		    	xmlUniversity.Field = university.field;
		    	xmlUniversity.Qualification = university.qualification;
			    if(university.yearCompleted == "")
			    {
			    	delete xmlUniversity.YearCompleted;
			    }
			    else
			    {
			    	xmlUniversity.YearCompleted = university.yearCompleted;
			    }
		    	xmlUniversity.DocumentName = university.documents;
		    	xmlUniversity.DocumentState = university.documentState.toInt();
		    	
		    	xml.insertChildBefore(xml.Notes, xmlUniversity)
		    }

		    var idProf:int = 0;
		    for each (var professional:QualificationRecord in professionalQualifications)
		    {
		    	var xmlProfessional:XML = xmlProfessionalTemplate.copy();

		    	xmlProfessional.id = idProf++;
		    	xmlProfessional.InstitutionName = professional.institution;
		    	xmlProfessional.Field = professional.field;
		    	xmlProfessional.Qualification = professional.qualification;
			    if(professional.yearCompleted == "")
			    {
			    	delete xmlProfessional.YearCompleted;
			    }
			    else
			    {
			    	xmlProfessional.YearCompleted = professional.yearCompleted;
			    }
		    	xmlProfessional.DocumentName = professional.documents;
		    	xmlProfessional.DocumentState = professional.documentState.toInt();
		    	
		    	xml.insertChildBefore(xml.Notes, xmlProfessional)
		    }

		    var idInServ:int = 0;
		    for each (var inService:QualificationRecord in inServiceTraining)
		    {
		    	var xmlInService:XML = xmlInServiceTemplate.copy();

		    	xmlInService.id = idInServ++;
		    	xmlInService.TrainingCourse = inService.field;
		    	xmlInService.Institution = inService.institution;
			    if(inService.yearCompleted == "")
			    {
			    	delete xmlInService.YearCompleted;
			    }
			    else
			    {
			    	xmlInService.YearCompleted = inService.yearCompleted;
			    }
		    	xmlInService.DocumentName = inService.documents;
		    	xmlInService.DocumentState = inService.documentState.toInt();
		    	
		    	xml.insertChildBefore(xml.Notes, xmlInService)
		    }
		    for each (var note:ObservationsOpinions in observationsOpinions)
		    {
		    	var xmlNote:XML = xmlNoteTemplate.copy();
			    xmlNote.RelatedSection = note.relatedSection;
			    xmlNote.NoteRef = note.noteRef;
			    xmlNote.Observation = note.observation;
			    xmlNote.Opinion = note.opinion;
			    xml.Notes.insertChildBefore(xml.Notes.DateObservations, xmlNote);
		    }
		    
			if(observerDate == null || isNaN(observerDate.date))
		    {
		    	delete xml.Notes.DateObservations;
		    }
		    else
		    {
	      		xml.Notes.DateObservations = df.format(observerDate);
		    }
      		xml.Notes.ObserverName = observerName;
      		xml.Notes.ObserverSign = booleanToInt(observerSign);
			if(opinionGiverDate == null || isNaN(opinionGiverDate.date))
		    {
		    	delete xml.Notes.DateOpinionGiven;
		    }
		    else
		    {
	      		xml.Notes.DateOpinionGiven = df.format(opinionGiverDate);
		    }
      		xml.Notes.OpinionGiverName = opinionGiverName;
      		xml.Notes.OpinionGiverSign = booleanToInt(opinionGiverSign); 

			//History and Actions
		
			for each (var history:CaseHistory in CaseHistoryOne)
		    {
		    	var dfTimestamp2:DateFormatter = new DateFormatter();
				dfTimestamp2.formatString = "YYYY/MM/DD";
		    	var xmlHistory:XML = xmlCaseHistoryTemplate.copy();
		    	xmlHistory.Date = dfTimestamp2.format(history.DateNew);
			    xmlHistory.AssignedTo = history.AssignedTo;
			    xmlHistory.CreatedDate = history.CreatedDate;
			    //xmlHistory.ModifiedBy = history.ModifiedBy;
			    xmlHistory.CaseID = history.CaseID;
			    xmlHistory.ECNo = history.ECNo;
			    xmlHistory.CaseTypeID = history.CaseTypeID;
			    xmlHistory.hasChanged = history.hasChanged;
			    xml.insertChildBefore(xml.RespondentSign, xmlHistory);
		    }
		    	
			for each (var actions:CaseActions in CaseActionsOne)
		    {
		    	var dfTimestamp4:DateFormatter = new DateFormatter();
				dfTimestamp4.formatString = "YYYY/MM/DD";
		    	var xmlAction:XML = xmlCaseActionsTemplate.copy();
			    xmlAction.Date = dfTimestamp4.format(actions.DateNewAction);
			    xmlAction.CreatedBy = actions.CreatedBy;
			    xmlAction.OfficialResponsible = actions.OfficialResponsible;
			    xmlAction.Outcome = actions.Outcome;
			    xmlAction.CaseID = actions.CaseNumber;
			    xmlAction.CreatedDate = actions.CreatedDate;
			    xmlAction.ActionTaken = actions.ActionTaken;
			   // xmlAction.ModifiedBy = actions.ModifiedBy;
			    xmlAction.ID = actions.ID;
			    xmlAction.hasChangedAction = actions.hasChangedAction;
			    xml.insertChildBefore(xml.RespondentSign, xmlAction);
		    }
		    	
		    xml.RespondentSign = booleanToInt(respondentSign);
		    xml.AuditorName = auditorName;
		    xml.AuditorSign = booleanToInt(auditorSign);
		    xml.DataEntryUserName = dataEntryUserName;
		    xml.DataEntryUserSign = booleanToInt(dataEntryUserSign);
		    if(dataEntryDate == null || isNaN(dataEntryDate.date))
		    {
		    	delete xml.DataEntryDate;
		    }
		    else
		    {
		    	xml.DataEntryDate = dfTimestamp.format(dataEntryDate);
		    }
		    xml.DataVerificationUserName = dataVerificationUserName;
		    xml.DataVerificationUserSign = booleanToInt(dataVerificationUserSign) as int;
		    //OLD WAY xml.DataVerificationUserSign = dataVerificationUserSign ? "true" : "false";
		    if(dataVerificationDate == null || isNaN(dataVerificationDate.date))
		    {
		    	delete xml.DataVerificationDate;
		    }
		    else
		    {
		    	xml.DataVerificationDate = dfTimestamp.format(dataVerificationDate);
		    }
		    xml.StatusCode = statusCode;
		    xml.Rejected = booleanToInt(rejected) as int;
		    xml.VerifierComment = verifierComment;
			return xml;
		}
	}
};
